package com.webchat.chat.core.tools;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Owner on 2018/6/29.
 */
public class ClientUtil {
    /**
     * 获取sessionID
     * @param request
     * @return
     */
    public static String getSessionId(HttpServletRequest request) {
        return (String)request.getSession().getId();
    }

    public static String getOSInfo(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent").toLowerCase();
        String os = "";
        if (userAgent.toLowerCase().indexOf("windows") >= 0) {
            os = "Windows";
        } else if (userAgent.toLowerCase().indexOf("mac") >= 0) {
            os = "Mac";
        } else if (userAgent.toLowerCase().indexOf("x11") >= 0) {
            os = "Unix";
        } else if (userAgent.toLowerCase().indexOf("android") >= 0) {
            os = "Android";
        } else if (userAgent.toLowerCase().indexOf("iphone") >= 0) {
            os = "IPhone";
        } else {
            os = "UnKnown, More-Info: " + userAgent;
        }

        return os;
    }

    /**
     * 返回客户端浏览器的版本号、类型
     */
    public static String getBrowser(HttpServletRequest request) {
        String browserInfo = "other";
        String ua = request.getHeader("User-Agent").toLowerCase();
        String s;
        String version;
        String msieP = "msie ([\\d.]+)";
        String ieheighP = "rv:([\\d.]+)";
        String firefoxP = "firefox\\/([\\d.]+)";
        String chromeP = "chrome\\/([\\d.]+)";
        String operaP = "opr.([\\d.]+)";
        String safariP = "version\\/([\\d.]+).*safari";

        Pattern pattern = Pattern.compile(msieP);
        Matcher mat = pattern.matcher(ua);
        if (mat.find()) {
            s = mat.group();
            version = s.split(" ")[1];
            browserInfo = "ie " + version.substring(0, version.indexOf("."));
            return browserInfo;
        }

        pattern = Pattern.compile(firefoxP);
        mat = pattern.matcher(ua);
        if (mat.find()) {
            s = mat.group();
            version = s.split("/")[1];
            browserInfo = "firefox "
                    + version.substring(0, version.indexOf("."));
            return browserInfo;
        }

        pattern = Pattern.compile(ieheighP);
        mat = pattern.matcher(ua);
        if (mat.find()) {
            s = mat.group();
            version = s.split(":")[1];
            browserInfo = "ie " + version.substring(0, version.indexOf("."));
            return browserInfo;
        }

        pattern = Pattern.compile(operaP);
        mat = pattern.matcher(ua);
        if (mat.find()) {
            s = mat.group();
            version = s.split("/")[1];
            browserInfo = "opera " + version.substring(0, version.indexOf("."));
            return browserInfo;
        }

        pattern = Pattern.compile(chromeP);
        mat = pattern.matcher(ua);
        if (mat.find()) {
            s = mat.group();
            version = s.split("/")[1];
            browserInfo = "chrome "
                    + version.substring(0, version.indexOf("."));
            return browserInfo;
        }

        pattern = Pattern.compile(safariP);
        mat = pattern.matcher(ua);
        if (mat.find()) {
            s = mat.group();
            version = s.split("/")[1].split(" ")[0];
            browserInfo = "safari "
                    + version.substring(0, version.indexOf("."));
            return browserInfo;
        }
        return browserInfo;
    }

    /**
     * 获得真实IP地址。在使用了反向代理时，
     * 直接用HttpServletRequest.getRemoteAddr()无法获取客户真实的IP地址。
     * @param request
     * @return
     */
    public static String getRemoteAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        //2016-05-01 ml 处理多IP地址，截取第一个用户IP
        if(ip!=null){
            String[] ips = ip.split(",", -1);
            if(ips.length>1){
                ip = ips[0];
            }
        }

        return ip;
    }

    /**
     * 获取域名
     */
    public static String getDomain(HttpServletRequest req) {
        String domain = req.getScheme() + "://" + req.getServerName();
        return domain;
    }

    public static String getDomainNoHttp(HttpServletRequest req) {
        return req.getServerName();
    }

}
